const BRANCHSIGN = ','

const TEMPLATE = {
  branch: 'branch',
  uid: 'uid',
  id: 'id',
  name: 'name',
  itemUid: 'item_uid',
  itemId: 'item_id',
  itemName: 'item_name',
  parent: 'parent_uid'
}

export function getChildren(data, root, template = TEMPLATE) {
  if (!data) {
    return
  }
  const children = data.filter(e => e.parent_uid && e.parent_uid === root.uid).map(e => {
    return { ...e, children: getChildren(data, e, template) }
  })

  return children
}
export function getRoot(data, template = TEMPLATE) {
  if (!data) {
    return []
  }
  const result = data.filter(item => !item.parent_uid)
  for (const item of data.filter(item => item.parent_uid)) {
    let hasParent = false
    for (const i of data) {
      if (item[template.parent] === i[template.uid] || item[template.parent] === i[template.itemUid]) {
        hasParent = true
        break
      }
    }
    if (!hasParent) {
      result.push(item)
    }
  }
  return result
}

export function getGenealogyFormat(data, template = TEMPLATE) {
  if (!data) {
    return
  }
  let root = getRoot(data, template)
  root = root.map(item => {
    return { ...item, children: getChildren(data, item, template) }
  })
  return root
}
export function filterByValue(data, filter, template = TEMPLATE) {
  const filterValue = filter.trim().toLowerCase()
  return data.filter(item =>
    (template.uid && item[template.uid] && item[template.uid].toLowerCase().indexOf(filterValue) !== -1) ||
    (template.itemUid && item[template.itemUid] && item[template.itemUid].toLowerCase().indexOf(filterValue) !== -1) ||
    (template.id && item[template.id] && item[template.id].toLowerCase().indexOf(filterValue) !== -1) ||
    (template.name && item[template.name] && item[template.name].toLowerCase().indexOf(filterValue) !== -1) ||
    (template.itemId && item[template.itemId] && item[template.itemId].toLowerCase().indexOf(filterValue) !== -1) ||
    (template.itemName && item[template.itemName] && item[template.itemName].toLowerCase().indexOf(filterValue) !== -1)
  )
}

export function filterList(data, filter, template = TEMPLATE) {
  if (filter && filter.trim()) {
    return filterByValue(data, filter, template)
  } else {
    return data
  }
}

export function filterBranch(data, filter, template = TEMPLATE) {
  if (filter && filter.trim()) {
    const list = filterByValue(data, filter, template).reduce(
      (result, item) => ([...result, ...(template.branch && item[template.branch] ? item[template.branch]
        : (template.uid && item[template.uid] ? item[template.uid]
          : (template.itemUid && item[template.itemUid] ? item[template.itemUid]
            : (template.id && item[template.id] ? item[template.id]
              : (template.itemId && item[template.itemId] ? item[template.itemId]
                : (template.name && item[template.name] ? item[template.name]
                  : (template.itemName && item[template.itemName] ? item[template.itemName]
                    : []))))))).split(BRANCHSIGN)])
      , [])
    return data.filter(item =>
      (template.uid && list.indexOf(item[template.uid]) !== -1) ||
      (template.itemUid && list.indexOf(item[template.itemUid]) !== -1) ||
      (template.id && list.indexOf(item[template.id]) !== -1) ||
      (template.itemId && list.indexOf(item[template.itemId]) !== -1) ||
      (template.name && list.indexOf(item[template.name]) !== -1) ||
      (template.itemName && list.indexOf(item[template.itemName]) !== -1)
    )
  } else {
    return data
  }
}

export default {
  getRoot,
  filterList,
  filterBranch,
  filterByValue
}
